<template>
	<view>
		<block>
			<uni-nav-bar @clickLeft="goBack" status-bar="true" shadow="true" left-icon="back" left-text="返回" :title="title"></uni-nav-bar>
		</block>
		<uni-list>
			<uni-swipe-action style="background-color: #F1F1F1;">
				<scroll-view :style="{height:scrowviewheight+'px'}" scroll-y="true" @scrolltolower="loadmore()">
					<block v-for="(item,index2) in myTopics" :key="index2">
						<uni-swipe-action-item :options="item.isOpen?options1:options2" @click="bindClick(item,index2)">
							<index-list class="card" :class="{'item-topic': !item.isOpen }" :item="item" :index="index2" :from="title"></index-list>
							<!-- <card  class="card" :cardinfo="cardinfo"></card> -->
						</uni-swipe-action-item>
					</block>
				</scroll-view>
			</uni-swipe-action>
		</uni-list>


	</view>
</template>

<script>
	import indexList from "../../components/index/index-list.vue";
	import uniList from '@/components/uni-list/uni-list.vue'
	import uniListItem from '@/components/uni-list-item/uni-list-item.vue'
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	import config from "@/common/config.js"
	import uniSwipeAction from '@/components/uni-swipe-action/uni-swipe-action.vue'
	import uniSwipeActionItem from '@/components/uni-swipe-action-item/uni-swipe-action-item.vue'
	import card from '@/components/list-card/list-card.vue'
	export default {
		components: {
			indexList,
			uniNavBar,
			uniList,
			uniListItem,
			uniSwipeAction,
			uniSwipeActionItem,
			card
		},
		data() {
			return {
				myTopics: [],
				options1: [{
					text: '隐藏',
					style: {
						backgroundColor: '#dd524d'

					}
				}],
				options2: [{
					text: '公开',
					style: {
						backgroundColor: '#4cd964'
					}
				}],
				scrowviewheight: 300,
				pageId: 0,
				title: '我的记录',
				cardinfo: {
					"authImg": '图片地址',
					"authName": "张三",
					"createTime": "2020-02-01",
					"content": " 内容",
					"goodNum": "100",
					"likeNum": "200",
					"commentNum": "500",
					"leave": 8,
					"tag": "每日随笔"
				}


			}
		},

		onLoad(option) {
			uni.getSystemInfo({
				success: (res) => {
					//减去顶部tab，底部tabbar，顶部搜索,顶部搜索margin-top
					let windowHight = res.windowHeight - uni.upx2px(100) - uni.upx2px(100);
					this.scrowviewheight = windowHight;
				}
			});
			this.pageId = option.id;
			if (this.pageId == 1) {
				this.title = '我的收藏';
				this.options1[0].text = '删除'
			}
			this.getTopicsOfTopicer();
		},
		onPullDownRefresh() {},
		methods: {
			loadmore() {

			},
			getTopicsOfTopicer() {
				var url = config.baseUrl + '/users/mytopics'; //没必要做加载更多
				if (this.pageId == 1) {
					url = config.baseUrl + '/users/mycollectedtopics';
				}
				var token = uni.getStorageSync(config.token);
				uni.request({
					method: 'GET',
					url: url,
					header: {
						'content-type': 'application/json', //自定义请求头信息
						'Authorization': 'Bearer ' + token //通过登录接口，生成新的token
					},
					success: (res) => {
						var statusCode = res.statusCode;
						console.log(statusCode);
						if (200 == statusCode) {
							console.log("wod list");
							console.log(res.data);
							this.myTopics = res.data;
						} else if (401 == statusCode) {
							this.checkSession();
						} else {}
					},
					fail: (res) => {}
				});

			},
			checkSession() {
				uni.checkSession({
					success: (e) => {
						var weixinsession = uni.getStorageSync(config.sessionkey);
						if (weixinsession.length > 0) {
							uni.navigateTo({
								url: '../loginnew/loginnew'
							})
						} else {
							uni.navigateTo({
								url: '../authorityRequst/authorityRequst'
							})
						}

					},
					fail: () => {
						console.log("yijinggoqdi")
						uni.navigateTo({
							url: '../authorityRequst/authorityRequst'
						})
					}
				});
			},
			//取消收藏
			uncollecte(item, index2) {
				uni.showLoading({
					title: 'loading'
				});
				var method = 'DELETE';
				var action = 'uncollecte';
				if (this.pageId == 0) {
					method = 'GET';
					action = 'hideoropentopic';
				}
				var id = item._id;
				var url = config.baseUrl + '/users/' + action + '/' + id;
				console.log(url);
				var token = uni.getStorageSync(config.token);
				uni.request({
					method: method,
					url: url,
					header: {
						'content-type': 'application/json', //自定义请求头信息
						'Authorization': 'Bearer ' + token //通过登录接口，生成新的token
					},
					success: (res) => {
						var statusCode = res.statusCode;
						console.log(statusCode);
						if (200 == statusCode) {
							var title = '';
							if (this.pageId == 1) {
								title = '已删除';
								this.myTopics.splice(index2, 1);
							}
							if (this.pageId == 0) {

								this.myTopics[index2].isOpen = !this.myTopics[index2].isOpen;
								if (this.myTopics[index2].isOpen) {
									title = '已公开';
								}
								if (!this.myTopics[index2].isOpen) {
									title = '已隐藏';
								}
							}
							console.log("成功ppp");
							uni.showToast({
								title: title,
								icon: 'none'
							})
						} else if (401 == statusCode) {
							this.checkSession();
						} else {
							console.log("wod err");
						}
					},
					fail: (res) => {
						console.log("wod fafas");
					},
					complete: () => {
						uni.hideLoading();
					}
				});
			},

			bindClick(item, index2) {
				console.log("bindClick");
				this.uncollecte(item, index2);
			},
			goBack() {
				uni.navigateBack({

				})
			}
		}
	}
</script>

<style>
	

	.uni-swipe_button {
		margin-top: 30upx;
		margin-bottom: 30upx;
	}

	.card {
		width: 100%;
		margin-left: 30upx;
		margin-right: 30upx;
	}

	.uni-swipe_box {
		background-color: #F1F1F1 !important;
	}
</style>
